package com.cocoiko;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewTreeObserver.OnScrollChangedListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.cocoiko.customview.ShopListAdapter;
import com.cocoiko.info.ShopInfo;
import com.cocoiko.tools.shoputils.ShopInfoRequest;
import com.cocoiko.utils.CocoikoUtils;

public class ShopSelect extends Activity implements OnClickListener,
		OnItemClickListener, OnKeyListener {

	Button back_button;
	ImageButton reload_button;
	EditText area_textview;
	EditText keyword_textview;
	ListView shop_listview;

	ShopListAdapter adapter;
	ArrayList<ShopInfo> shops;

	int page = 1;
	Location location;

	ProgressDialog progress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_select);

		back_button = (Button) findViewById(R.id.back);
		reload_button = (ImageButton) findViewById(R.id.reload);
		area_textview = (EditText) findViewById(R.id.area);
		keyword_textview = (EditText) findViewById(R.id.keyword);
		shop_listview = (ListView) findViewById(R.id.list);

		back_button.setOnClickListener(this);
		reload_button.setOnClickListener(this);
		area_textview.setOnClickListener(this);
		keyword_textview.setOnClickListener(this);
		shop_listview.setOnItemClickListener(this);

		keyword_textview.setOnKeyListener(this);
		area_textview.setOnKeyListener(this);
		// get location
		location = CocoikoUtils.getCurrentLocation(this);

		// initial
		initialListView();

		// load shop info
		loadNextShopInfoThread();
	}

	Handler nextShopInfoHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			progress.dismiss();
			adapter.notifyDataSetChanged();
		};
	};

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if (view == back_button) {
			finish();
		} else if (view == reload_button) {
			//
			reloadButtonClick();
		} else if (view == area_textview) {
			area_textview.setText("");
		} else if (view == keyword_textview) {
			keyword_textview.setText("");
		}

	}

	public void initialListView() {

		shops = new ArrayList<ShopInfo>();
		ShopInfo info = new ShopInfo(null, "", "more ...");
		shops.add(info);

		adapter = new ShopListAdapter(this, R.layout.shop_listview_item, shops);
		shop_listview.setAdapter(adapter);
	}

	/**
	 * 
	 */
	public void loadNextShopInfo() {
		if (location != null) {

			ArrayList<ShopInfo> infs = ShopInfoRequest.getNearShop(
					location.getLatitude(), location.getLongitude(), page++);

			if (infs != null) {
				shops.addAll(shops.size() - 1, infs);
			}
		}
	}

	/**
	 * 
	 */
	public void loadNextShopInfoThread() {

		progress = ProgressDialog.show(this, "", "Loading ...");
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				loadNextShopInfo();
				nextShopInfoHandler.sendEmptyMessage(0);
			}
		}).start();
	}

	public void searchShop(){
		
	}
	// reload shop list
	public void reloadButtonClick() {
		shops.clear();
		initialListView();
		page = 1;
		loadNextShopInfoThread();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long arg3) {
		// TODO Auto-generated method stub
		if (position == shops.size() - 1) {
			loadNextShopInfoThread();
		}

	}

	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (v == area_textview) {
			
			if (keyCode == KeyEvent.KEYCODE_ENTER) {
				Log.i("cursor", "changed");
				keyword_textview.setCursorVisible(true);
			}
		} else if (v == keyword_textview) {
			if (keyCode == event.KEYCODE_ENTER) {
				searchShop();
			}
		}

		return true;
	}

}
